#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Library for common used functions
'''
from __future__ import division

import sys
import os
import numpy
import re
import math

def list_choice(list_args):
    '''
    The same idea as for choice function, but we play only with simple list, without choices
    '''
    
    if len(list_args) == 0:
        raise 'Zero variants. No choice'
    elif len(list_args) == 1:
        return [ list_args[0] ]
    else:
        for i, case in enumerate( list_args ):
            print i,  ' - ',  case
        caseNumber = raw_input('Enter number of the case we are interested in\n')
        try:
            caseNumber = [ int(caseNumber) ]
        except:
            caseNumber = [ int(j) for j in caseNumber.strip().split() ]

        return [ list_args[j] for j in caseNumber ]  
        

def choice(path):
    '''
    path - path to the directory in which we are interested in. Function analyzes what subdirectories are in our directory
    and if there is more then one, asks user in which of them we are interested. As a result we have path to subdirectory    
    '''
    fList = os.listdir( path )
    fList.sort()
    if len(fList) == 0:
        raise 'Zero variants. No choice'
    elif len(fList) == 1:
        return fList[0]
    else:
        for i, case in enumerate( fList ):
            print i,  ' - ',  case
        
        tt = raw_input('Enter number of the case we are interested in\n')
        try:
            caseNumber = int( tt )
            return fList[ caseNumber]
        except:
            caseNumber = tt.strip().split()
            return [ fList[ int( ttt) ] for ttt in caseNumber ]
        
def argument(list_args):
    '''
    if list_args in sys.argv returns argument of list_args
    else returns False
    '''
    if list_args in sys.argv:
        try:
            return sys.argv[ sys.argv.index(list_args) + 1 ]
        except:
            return True
    else:
        return False

def sorted_list(path):
    '''
    this function is used to analyze data that I generate in my Monte Carlo simulations.
    input parameter - path in which we analyze data
    
    returns 
    
    list with temperature  
    list with corresponding values
    list with errorbars
    '''    
    tList = os.listdir(path)
    result = {}

    temp = []

    for tT in tList:
        m = re.search( '\d+.\d*' ,tT )        
        T = round(1000 * eval(m.group()))
        tPath = os.path.join(path, tT)
        try:
            value = eval(open(tPath).read())
        except:
            print T
        else:
            if T in result:
                result[T] += [value]
            else:
                result[T] = [value]
                
    for T in result:
        temp += [ (T, numpy.mean(result[T]), numpy.std(result[T]) / math.sqrt(len(result[T]))) ]

        
    
    temp.sort()
    x = [tx[0] / 1000 for tx in temp]
    y = [tx[1] for tx in temp]
    z = [tx[2] for tx in temp]
    
    return x, y, z

def derivative(xRange, yRange):
    '''
    for given function returns it's derivative and corresponding xValues 
    '''
    
    resultX = []
    resultY = []
    xRange.sort()
    yRange.sort()
    
    for i in xrange(len(xRange) - 1):        
        resultX += [ (xRange[i + 1] + xRange[i]) / 2  ]
        resultY += [ (yRange[i + 1] - yRange[i]) / ( xRange[i + 1] - xRange[i] ) ]
        
    return resultX, resultY